package com.studyroom.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.studyroom.common.Result;
import com.studyroom.entity.Reservation;
import com.studyroom.service.ReservationService;
import com.studyroom.dto.SearchDTO;
import com.studyroom.common.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/reservations/")
@PreAuthorize("hasRole('ADMIN')")
public class AdminReservationController {

    @Autowired
    private ReservationService reservationService;

    @PostMapping("/search")
    public Result<IPage<Reservation>> search(@RequestBody SearchDTO params) {
        return Result.success(reservationService.search(params));
    }

    @PutMapping("/{id}/end")
    public Result<Boolean> endReservation(@PathVariable Long id) {
        return Result.success(reservationService.endReservation(id));
    }

    @DeleteMapping("/{id}")
    public Result<?> deleteReservation(@PathVariable Long id) {
        try {
            // 删除预约时需考虑更新座位状态
            reservationService.deleteReservation(id);
            return Result.success("预约删除成功");
        } catch (Exception e) {
            return Result.error("删除失败: " + e.getMessage());
        }
    }
} 